(function(){
	/**
	* 页面初始化，载入数据
	*/
	async function init() {
		//判断是用户本人还是其他会员
		var _address = getNode("info.js").tag;
		if (!_address) {
			_address = publicParam.address;
		}
		await loadManbers();
		var my = getMy();
		setOnclick(getNode("myTab"));
		//获取会员信息
		publicParam.member = getMemberByAddress(_address);
		if (publicParam.member.up != _address){
			//显示上级会员
			const _upmember = getMemberByAddress(publicParam.member.up);
			setNodeText("upMember",_upmember.name);
			//上级会员不是自己时可以点击
			getNode("upMember").onclick = () =>{
				goto("member/info",publicParam.member.up);
			};
		}else{
			//创始会员，显示上级也是自己
			setNodeText("upMember",publicParam.member.name);
		}
		//本人是正式会员，且浏览的是非正式会员，显示批准按钮
		if (my && my.isReal && !publicParam.member.isReal && _address!=publicParam.address && publicParam.member.approver.indexOf(publicParam.address)==-1){
			display("approval",true);
			getNode("approval").onclick = approval;
		}else display("approval",false);
		//显示其他信息
		setNodeText("userName",publicParam.member.name);
		setNodeText("menberID",publicParam.member.id,0);
		setNodeText("joinTime",publicParam.member.joinTime);
		setNodeText("memberType",publicParam.member.isReal?"正式会员":"准会员");
		setNodeText("address",_address);
		//如果是用户被人，判断是否已经领取过NFT证书
		if (_address == publicParam.address && publicParam.member.isReal && publicParam.member.NFTid==0){
			if(confirm("您还没有领取NFT会员证，是否现在领取？")){
				var param = {
					webapi:"getCard", 
					id:publicParam.member.id,
					joinTime:publicParam.member.joinTime, 
					name:publicParam.member.name
				};
				// 动态生成证书的图片，用元数据去铸造证书
				callAPI(param,getNFT);
			}
		}else{
			// 获取证书元数据，并显示图片
			getNFTMeta(publicParam.member.NFTid);
		}
		if (!publicParam.member.reason){
			// 还没有获取过该用户的其他信息
			callAPI({webapi:"getMemberData",id:publicParam.member.id}, function (data){
				publicParam.member.about = data.about;
				publicParam.member.role = data.role;
				publicParam.member.skill = data.skill;
				publicParam.member.email = data.email;
				publicParam.member.wechat = data.wechat;
				publicParam.member.phone = data.phone;
				publicParam.member.awards = data.awards;
				publicParam.member.reason = data.reason;
				publicParam.member.education = data.education;
				publicParam.member.experience = data.experience;
				loadPage("member/about","myTabContent");
			});
		}else{
			// 已经加载过的话就直接显示
			loadPage("member/about","myTabContent");
		}
		pageParam=[];
	}

	/**
	* 批准成为正式用户
	*/
	async function approval() {
		if(!publicParam.member.reason || publicParam.member.reason.length<50){
			alert(publicParam.member.name+"填写的入会理由少于50字。");
			return;
		}
		setLoading(1);
        rtn = await memberApproval("批准"+publicParam.member.name+"成为正式用户",publicParam.member.address);
		if (rtn){
			//添加投票人的地址，不知为何直接Push会出错
			var tmp = [];
			publicParam.member.approver.forEach(item => {tmp.push(item)});
			tmp.push(publicParam.address);
			publicParam.member.approver = tmp;
			display("approval",false);
			//当投票人数达标后成为正式会员
			if (typeof(rtn) == "string") {
				publicParam.member.isReal = true;
				setNodeText("memberType","正式会员");
			}
		}
		setLoading(0);
	}

	/**
	* 领取NFT证书
	*/
	async function getNFT (data) {
		//领取NFT证书，一个用户只需要执行一次
		setLoading(1);
        rtn = await memberGetNFT("领取NFT证书",data.url);
		if (rtn){
			await getNFTMeta(rtn);
		}
		setLoading(0);
	}

	/**
	* 取得证书的元数据后显示图片
	*/
	async function getNFTMeta (id) {
		if (id!=0){
			//已经获取了NFT证书
			if (publicParam.member.NFTurl){
				//已经取得了NFT图像地址
				getNode("memberNFT").src = publicParam.member.NFTurl;
			}else{
				//先从钱包中获取NFT证书的元数据
				setLoading(1);
				const url = await getNFTURI(id);
				//再从元数据中取得图像地址
				var param = {webapi:"getNFTMeta", url:url};
				callAPI(param, function (data){
					publicParam.member.NFTurl = data.image;
					var Protocol = window.location.protocol.split(':')[0];
					if (Protocol=="https") getNode("memberNFT").src = publicParam.member.NFTurl.replace("http:","https:");
					else getNode("memberNFT").src = publicParam.member.NFTurl;
					setLoading(0);
				});
			}
		}
	}

	/**
	* 切换不同的标签页
	*/
	function enable(div){
		var par = div.parentNode.parentNode;
		for(var i=0;i<par.children.length;i++){
			par.children[i].children[0].ariaSelected="false";
			par.children[i].children[0].className="nav-link ab-tab-item";
		}
		div.ariaSelected="true";
		div.className="nav-link active ab-tab-item";
	}
	
	/**
	* 设置每个标签页的点击事件
	*/ 
	function setOnclick(node){
		for(var i=0;i<node.children.length;i++){
			if (node.children[i].id.substr(0,6)=="member"){
				node.children[i].onclick = function(){
					enable(this);
					loadPage(this.id,"myTabContent");
				};
			}
			if(node.children[i].children.length>0) {
			  setOnclick(node.children[i]);
			}
		}
	}	
	
	if (publicParam.walletConnected) init();
})();